package com.eqdd.yiqidian.ui.tongxunlu;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.ui.ScanZXingActivity;
import com.eqdd.yiqidian.ui.base.BaseEnterActivity;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

/**
 * Created by lvzhihao on 17-4-7.
 */

public class AddFriendTypeActivity extends BaseEnterActivity {
    private static final int REQUEST_CODE = 1;
    private String[] contents = new String[]{
            "手机号/易企点号加好友",
            "扫一扫加好友",
            "邀请朋友注册易企点",
            "一键导入"
    };

    private int[] decoders = new int[]{10};


    @Override
    public void itemClick(int p) {
        switch (p) {
            case 0:
                RxPermissions.getInstance(AddFriendTypeActivity.this).request(android.Manifest.permission.READ_CONTACTS)
                        .subscribe(granted -> {
                            if (!granted) {
                                ToastUtil.showShort("请允许应用获取联系人信息");
                                Intent intent = new Intent();
                                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            } else {
                                startActivity(AddFriendFirstTypeActivity.class);
                            }
                        });
                break;
            case 1:
                RxPermissions.getInstance(this).request(Manifest.permission.CAMERA)
                        .subscribe(granted -> {
                            if (granted) {
                                Intent intent = new Intent(AddFriendTypeActivity.this, ScanZXingActivity.class);
                                startActivityForResult(intent, REQUEST_CODE);
                            } else {
                                ToastUtil.showLong("请在设置中开启照相权限");
                            }
                        });
                break;
            case 2:
                Intent intentSend = new Intent(Intent.ACTION_SEND);
                intentSend.setType("text/plain");
                intentSend.putExtra(Intent.EXTRA_SUBJECT, "分享");
                String s = TextUtils.isEmpty(AppConstent.user.getUsername()) ? "" : "(" + AppConstent.user.getUsername() + ")";
                intentSend.putExtra(Intent.EXTRA_TEXT, "【易企点】您的朋友:" + user.getUname() + s + "邀请您注册易企点,点击 https://www.eqidd.com/relatedLink/related.html 注册");
                startActivity(Intent.createChooser(intentSend, getTitle()));
                break;
            case 3:
                RxPermissions.getInstance(this).request(Manifest.permission.READ_CONTACTS)
                        .subscribe(granted -> {
                            if (granted) {
                                Intent intent = new Intent(AddFriendTypeActivity.this, ContantsImportActivity.class);
                                startActivity(intent);
                            } else {
                                ToastUtil.showLong("请在设置中开启读取联系人权限");
                            }
                        });
                break;


        }
    }

    /**
     * 【易企点】您的朋友:18281611381(梁新帅)邀请您注册易企点,点击 https://www.eqidd.com/relatedLink/related.html 注册
     */
    @Override
    public void initEnterData() {
        initTopTitleBar(View.VISIBLE, "添加好友");
        int index = 0;
        for (int i = 0; i < contents.length; i++) {
            EnterBean enterBean = new EnterBean();
            if (index < decoders.length && i == decoders[index]) {
                index++;
                enterBean.setBottom(true);
                enterBean.setUnderLine(false);
            }
            enterBean.setContent(contents[i]);
            enterBean.setIcon(false);
            enterBean.setEnter(true);
            enterBeans.add(enterBean);
        }
        dataBinding.recyclerView.addItemDecoration(
                new SectionDividerItemDecoration(this)
                        .setLeftPadding(DensityUtil.percentW(30))
                        .setRightPadding(DensityUtil.percentW(30)));
    }
}